import cv2
import cv2.aruco

# pip install opencv-contrib-python
# 生成标记 https://fodi.github.io/arucosheetgen

dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_250)
params = cv2.aruco.DetectorParameters_create()

url = 'http://admin:admin@192.168.74.213:8081'
camera = cv2.VideoCapture(url)
while(camera.isOpened()):
    ret, frame = camera.read()
    if ret:
        corners, ids, rejected = cv2.aruco.detectMarkers(frame, dict, parameters=params)
        if len(corners):
            # 打印第一个标签的四角坐标
            #print(corners[0])
            # 左上角坐标
            tl = corners[0][0][0]
            # 右上角
            tr = corners[0][0][1]
            # 右下角
            br = corners[0][0][2]
            # 左下角
            bl = corners[0][0][3]
            # 计算中心点
            c = (tl + tr + br + bl) / 4
            print(c)
            # 绘制中心点
            cv2.circle(frame, (int(c[0]), int(c[1])), 5, (0, 0, 255), 2)

        cv2.aruco.drawDetectedMarkers(frame, corners, ids)
        cv2.imshow('camera', frame)
    if cv2.waitKey(1) == ord('q'):
        break

camera.release()
cv2.destroyAllWindows()
